<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="用户编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="vip" prop="vipLevel">
        <el-select
          v-model="queryParams.vipLevel"
          placeholder="实名信息"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in vipOptions"
            :key="dict.id"
            :label="dict.vipName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="实名信息" prop="isRule">
        <el-select
          v-model="queryParams.isRule"
          placeholder="实名信息"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in ruleOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:user:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户手机号" align="center" prop="phone" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="scope">
          {{scope.row.sex == 0 ? '女' : '男'}}
        </template>
      </el-table-column>
      <el-table-column label="头像地址" align="center" prop="imgUrl" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="上级编号" align="center" prop="parentId" />
      <el-table-column label="状态" align="center" prop="isStatus" >
        <template slot-scope="scope">
          {{scope.row.sex == 0 ? '封禁' : '正常'}}
        </template>
      </el-table-column>
      <el-table-column label="vip 等级" align="center" prop="vipLevel" :formatter="vipFormatter" />
      <el-table-column label="实名状态" align="center" prop="isRule" >
        <template slot-scope="scope">
          {{scope.row.sex == 0 ? '未实名' : '实名'}}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="userCar" />
      <el-table-column label="身份证名字" align="center" prop="carName" />
      <el-table-column label="支付宝名称" align="center" prop="aliUserName" />
      <el-table-column label="支付宝账号" align="center" prop="aliAccount" />\
      <el-table-column label="银行卡持有人真实姓名" align="center" prop="bankName" />
      <el-table-column label="银行卡号" align="center" prop="bankCar" />
      <el-table-column label="邀请码" align="center" prop="sysHost" />
      <el-table-column label="用户登录密码" align="center" prop="passWord" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:user:remove']"
          >删除</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="findUserLevel(scope.row)"
            v-hasPermi="['admin:user:level']"
          >查看会员图谱
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入用户手机号" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="头像地址" prop="imgUrl">
          <el-image
            style="width: 100px; height: 100px"
            :src="form.imgUrl"
            :preview-src-list="[form.imgUrl]">
          </el-image>        </el-form-item>
        <el-form-item label="上级编号" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入上级编号" />
        </el-form-item>
        <el-form-item label="vip 等级" prop="vipLevel">
          <el-select
            v-model="form.vipLevel"
            placeholder="实名信息"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in vipOptions"
              :key="dict.id"
              :label="dict.vipName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户状态" prop="isStatus">
          <el-select
            v-model="form.isStatus"
            placeholder="实名信息"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.val"
              :label="dict.lable"
              :value="dict.val"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="实名状态" prop="isRule">
          <el-select
            v-model="form.isRule"
            placeholder="实名信息"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in ruleOptions"
              :key="dict.val"
              :label="dict.lable"
              :value="dict.val"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="userCar">
          <el-input v-model="form.userCar" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="身份证名字" prop="carName">
          <el-input v-model="form.carName" placeholder="请输入身份证名字" />
        </el-form-item>
        <el-form-item label="支付宝名称" prop="aliUserName">
          <el-input v-model="form.aliUserName" placeholder="请输入支付宝名称" />
        </el-form-item>
        <el-form-item label="支付宝账号" prop="aliAccount">
          <el-input v-model="form.aliAccount" placeholder="请输入支付宝账号" />
        </el-form-item>
        <el-form-item label="邀请码" prop="sysHost">
          <el-input v-model="form.sysHost" placeholder="请输入邀请码" />
        </el-form-item>
        <el-form-item label="用户登录密码" prop="passWord">
          <el-input v-model="form.passWord" placeholder="请输入登录密码" />
        </el-form-item>

        <el-form-item label="银行卡持有人真实姓名" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入银行卡持有人真实姓名" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankCar">
          <el-input v-model="form.bankCar" placeholder="请输入银行卡号" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog
      :title="userLevelTitle"
      :visible.sync="userLevelShow"
      width="1300px"
      append-to-body
    >
      <el-tabs v-model="editableTabsValue" type="card" @tab-click="tabClick" closable @tab-remove="removeTab">
        <el-tab-pane
          v-for="(item, index) in editableTabs"
          :key="item.name"
          :label="item.title"
          :name="item.name"
          :closable="item.disabled"
        >

          <el-table
            stripe
            v-loading="loading"
            :data="item.content"
          >

            <el-table-column
              label="直推数量序号"
              type="index"
              width="50">
            </el-table-column>
            <el-table-column
              label="用户名称"
              prop="userName"
              width="180"
              align="center"
            >
              <template slot-scope="scope">
                <el-button v-if="tempUserId != scope.row.id" type="primary" @click="appendUserLevel(scope.row)" round>{{scope.row.userName}}</el-button>
                <span v-else>{{scope.row.userName}}</span>
              </template>
            </el-table-column>


            <el-table-column
              label="用户手机号"
              align="center"
              prop="phone"
            />

            <el-table-column label="实名状态" align="center" prop="isRule" >
              <template slot-scope="scope">
                {{scope.row.sex == 0 ? '未实名' : '实名'}}
              </template>
            </el-table-column>

            <el-table-column
              label="用户等级"
              align="center"
              prop="levelName"
              :formatter="vipFormatter"
            />







            <el-table-column
              label="用户头像"
              align="center"
              prop="imgUrl"
            >
              <template slot-scope="scope">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="scope.row.imgUrl"
                  :preview-src-list="[scope.row.imgUrl]"
                >
                </el-image>
              </template>
            </el-table-column>


          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, exportUser } from "@/api/admin/user";
import { listVip_level} from "@/api/admin/vip_level";

export default {
  name: "User",
  data() {
    return {
      sexOptions:[
        {
          lable:'男',
          val:1
        },
        {
          lable:'女',
          val:0
        },
      ],
      ruleOptions:[
        {
          lable:'实名',
          val:1
        },
        {
          lable:'未实名',
          val:0
        },
      ],
      statusOptions:[
        {
          lable:'正常',
          val:1
        },
        {
          lable:'封禁',
          val:0
        },
      ],
      vipOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      editableTabs:[],
      userLevelShow:false,
      userLevelTitle:'',
      editableTabsValue:'1',
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      tempUserId:"",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,

        userName: null,

        sex: null,

        imgUrl: null,

        parentId: null,

        isType: null,

        isStatus: null,

        vipLevel: null,

        isRule: null,

        userCar: null,

        carName: null,

        aliUserName: null,

        aliAccount: null,

        sysHost: null,

        passWord: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    listVip_level({pageNum: 1,pageSize: 1000000}).then(res =>{
      this.vipOptions = res.rows;
    })
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    vipFormatter(row){
      var tempName = '';
      this.vipOptions.forEach(item =>{
        if (row.vipLevel == item.id)
          tempName =  item.vipName;
      });
      return tempName;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    tabClick(targetName){
      this.tempUserId = targetName.paneName

    },

    removeTab(targetName) {
      let tabs = this.editableTabs;
      let activeName = this.editableTabsValue;
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            let nextTab = tabs[index + 1] || tabs[index - 1];
            if (nextTab) {
              activeName = nextTab.name;
            }
          }
        });
      }
      this.editableTabsValue = activeName;
      this.editableTabs = tabs.filter(tab => tab.name !== targetName);
      this.tempUserId = activeName
    },
    tableRowClassName({row, rowIndex}) {
      if (row.isUse  = 1) {
        return 'success-row';
      }
      return '';
    },
    // 查询会员图谱哦
    findUserLevel(row){
      this.editableTabs = []
      this.userLevelShow = true;
      this.userLevelTitle = `查看手机号为: ${row.phone} \r的用户级别关系.`

      listUser({pageSize: 10000,pageNum: 1,parentId:row.id}).then(res =>{
        if (res.rows.length == 0 ){
          this.msgSuccess("暂无下级");
          return;
        }
        this.tempUserId = row.id
        this.addTab(row.id,res.rows,true);
      })
    },
    appendUserLevel(row){
      this.userLevelTitle = `查看手机号为: ${row.phone} \r的用户级别关系.`
      listUser({pageSize: 10000,pageNum: 1,parentId:row.id}).then(res =>{
        if (res.rows.length == 0 ){
          this.msgSuccess("暂无下级");
          return;
        }
        this.tempUserId = res.rows[0].id
        this.addTab(row.id,res.rows,false);
      })
    },
    addTab(targetName,userLevelList,disable) {
      let newTabName = targetName;
      this.editableTabs.push({
        title: targetName,
        name: targetName,
        content: userLevelList,
        disable:disable
      });
      this.editableTabsValue = newTabName;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        phone: null,

        userName: null,

        sex: null,

        imgUrl: null,

        parentId: null,

        isType: null,

        isStatus: 0,

        vipLevel: null,

        createTime: null,

        isRule: null,

        userCar: null,

        carName: null,

        updateTime: null,

        aliUserName: null,

        aliAccount: null,

        sysHost: null,

        passWord: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
